<?php
/**
 * Created by PhpStorm.
 * Author: MeiJay
 * Date: 2018/11/5
 * Time: 18:07
 */

namespace App\Http\Controllers\Web\V1;


use App\Service\PrizeService;
use Illuminate\Http\Request;
use App\Exceptions\WebExceptions\WebException;
use Illuminate\Support\Facades\Validator;

class PrizeController extends BaseController
{


    /**
     * 奖品库分页列表
     * @param PrizeService $prizeService
     * @author: 梅杰 20181106
     *
     */
    public function prizes(PrizeService $prizeService)
    {
        $re = $prizeService->getAllList();
        success($re);
    }

    /**
     * 添加奖品
     * @throws WebException
     * @author: 梅杰 20101106
     */
    public function addPrize(Request $request)
    {
        $input = $request->only(['name','stock','imgs_ids','sponsor_id','price','desc','detail']);
        //数据验证
        $messages = [
            'name.required'     => '请填写奖品名称!',
            'stock.required'    => '请填写库存!',
            'imgs_ids.required' => '请添加奖品图片!',
            'sponsor_id.required' => '请选择赞助商',
            'price.required'       => '请填写价格'
        ];
        $validator = Validator::make($input,[
            'name' => 'required' ,
            'stock' => 'required' ,
            'imgs_ids' => 'required' ,
            'sponsor_id' => 'required',
            'price'     => 'required'
        ],$messages);

        if ($validator->fails()) {
            throw new WebException($validator->errors());
        }
        if ((new PrizeService())->add($input)) {
            success();
        }
        throw new WebException('添加失败');
    }

    /**
     * @param $id
     * @throws WebException
     * @author: 梅杰 20181106
     */
    public function updatePrize($id)
    {
        $input = app('request')->only(['name','stock','imgs_ids','sponsor_id','price','desc','detail']);

        //数据验证
        $messages = [
            'name.required' => '请填写奖品名称!',
            'stock.required' => '请填写库存!',
            'imgs_ids.required' => '请添加奖品图片!',
            'sponsor_id.required' => '请选择赞助商',
            'price.required'  => '请填写价格'
        ];
        $validator = Validator::make($input,[
            'name' => 'required' ,
            'stock' => 'required' ,
            'imgs_ids' => 'required' ,
            'sponsor_id' => 'required',
            'price'     => 'required'
        ],$messages);

        if ($validator->fails()) {
            throw new WebException($validator->errors());
        }
        $service = (new PrizeService());
        if ($service->saveRow($id,$input) !== false) {
            success();
        }
        throw new WebException('保存失败');
    }

    /**
     * 获取奖品详情
     * @param $id
     * @author: 梅杰 20181106
     */
    public function getPrize($id)
    {
        $service = (new PrizeService());
        $data = $service->getRow($id);
        success($data);
    }

    /**
     * 删除奖品
     * @param $id
     * @throws WebException
     * @author: 梅杰 20181106
     */
    public function deletePrize($id)
    {
      if ( (new PrizeService())->del($id) !== false) {
          success();
      }
        throw new WebException('删除失败');
    }



}